<!DOCTYPE html>
<html>

<head>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>
    <div id="container"></div>
</body>
<script>
    // var api="http://27.124.40.31:3456";
    var api = "http://134.122.135.9:3456";
    var list, servers;

    function start() {
        $.get("http://47.240.26.163:8989/list").done(function (data) {
            servers = data.split(",");
            servers = servers.filter(function (item) {
                return item != "134.122.135.9:16666" &&
                    item != "107.148.219.19:16666" &&
                    item != "47.56.31.109:16666" &&
                    item != "47.240.26.163:16666" &&
                    item != "47.244.182.194:16666";
            })
            $.post(api + "/api/video/sendRecommendVideos").then(
                function (data, status) {
                    list = JSON.parse(data.data);
                    if (list.length < 1) {
                        alert("没有数据")
                        return
                    }
                    handle()
                }, function () { });
        })
    };
    async function handle() {
        for (var idx = 0; idx < servers.length; idx++) {
            for (var i = 0; i < list.length; i++) {
                let src = list[i];
                let serv=servers[idx]
                // await create(src.url)
                await create(serv,src)
            }
        }
        $.post(api + "/api/video/saveRecommendVideo", { data: JSON.stringify(list) }).then(
            function (data, status) {
            }, function () { });
        start()
    }
    function create(server,src) {
        return new Promise((resolve, reject) => {
            $('#container').empty()
            let video = $('<video width="400" height="300" autoplay muted controls></video>');
            video.appendTo('#container');
            let url="http://"+server+"/proxy/"+src.base58Url+".mp4"
            video.attr("src", url).on("loadedmetadata", function () {
                setTimeout(function () {
                    video.get(0).pause()
                    console.log(video.get(0))
                    resolve('enough sleep~');
                }, 15000)
            }).on("error", function () {
                console.log('error')
                resolve('enough sleep~');
            })
        })
    }
    start()
</script>

</html>